package com.sales.controller;

import com.github.pagehelper.PageInfo;
import com.sales.client.StaffSalesClient;
import com.sales.service.InvoiceRegisterService;
import org.comple.erp.pojo.sales.InvoiceRegister;
import org.comple.erp.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class InvoiceRegisterController {
    @Autowired
    private InvoiceRegisterService invoiceRegisterServiceImpl;



    @RequestMapping("/selectInvoiceRegisterList")
    public AjaxResult selectInvoiceRegisterList(@RequestParam Map<String, Object> params){
        PageInfo<InvoiceRegister> invoiceRegisterList = invoiceRegisterServiceImpl.salectAll(params);
        return AjaxResult.back("200","查询成功").add("invoiceRegisterList",invoiceRegisterList);
    }

    @RequestMapping("/deleteInv")
    public AjaxResult deleteInv(@RequestParam List<String> invoiceNumber){
        Set<String> uniqueInvoiceNumbers = new HashSet<>(invoiceNumber);

        for(String inv : uniqueInvoiceNumbers){
            Boolean flag = invoiceRegisterServiceImpl.deleteByPrimaryKey(inv);
            if (!flag) {
                return AjaxResult.back("500", "删除失败");
            }
        }
        return AjaxResult.back("200", "删除成功");
    }


    @RequestMapping("/addInv")
    public AjaxResult addInv(@RequestBody List<InvoiceRegister> invoiceRecords){
        for(InvoiceRegister invoiceRecord : invoiceRecords){
            invoiceRecord.setId(UUID.randomUUID().toString());
            Boolean flag = invoiceRegisterServiceImpl.insertSelective(invoiceRecord);
            if (!flag) {
                return AjaxResult.back("500", "添加失败");
            }
        }
        return AjaxResult.back("200", "添加成功");
    }

    @RequestMapping("/checkInvNumber")
    public AjaxResult checkInvNumber(@RequestParam String invoiceNumber){
        int count = invoiceRegisterServiceImpl.selectCount(invoiceNumber);
        if(count>0){
            return AjaxResult.back("500", "发票号已存在");
        }
        return AjaxResult.back("200", "发票号可用");
    }
}
